package com.time.company.components.compose;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.time.company.R;

import java.util.ArrayList;
import java.util.List;

public class PoiListAdapter extends BaseAdapter{
    private Context ctx;
    private List<PoiItem> list;

    public PoiListAdapter(Context context, List<PoiItem> poiList) {
        this.ctx = context;
        this.list = poiList;
    }

    public void updateData(List<PoiItem> poiList){
        if (poiList == null){
            this.list = new ArrayList<>();
        }else {
            this.list = poiList;
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(ctx, R.layout.item_poi_list, null);
            holder.mtitle = (TextView) convertView.findViewById(R.id.tv_poi_one);
            holder.mSecondTitle = (TextView) convertView.findViewById(R.id.tv_poi_two);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PoiItem item = list.get(position);
        holder.mtitle.setText(item.getTitle());
        holder.mSecondTitle.setText(new StringBuilder().append(item.getCityCode() + " ").append(item.getAdName()));

        return convertView;
    }
    private class ViewHolder {
        TextView mtitle, mSecondTitle;
    }

}
